﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblComment.Text = "";
            }
            
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            var uid = txtUsername.Text.Trim();
            var pwd = txtPassword.Text.Trim();

            var sql = string.Format("select * from users where username='{0}'", uid);
            var dt = DbHelper.GetData(sql);
            if (dt.Rows.Count == 1)
            {
                var item = dt.Rows[0];

                var isActived = (bool)item.ItemArray[3];
                var isDeleted = (bool)item.ItemArray[4];
                var password = item.ItemArray[2].ToString();

                if (isDeleted)
                {
                    //提示当前用户删除
                    lblComment.Text = "当前用户己被删除，无法登录，有任何疑问，请联系管理人员";
                }
                else
                {
                    if (isActived)
                    {
                        if (pwd == password)
                        {
                            //用户密码正确，跳转到首页
                            //跳转
                            Response.Redirect("default.aspx");
                        }
                        else
                        {
                            //用户名或密码错误，请重试
                            lblComment.Text = "用户名或密码错误，请重试";
                        }
                    }
                    else
                    {
                        //提示当前用户被禁用或冻结
                        lblComment.Text = "当前用户己被禁用或冻结，有任何疑问，请联系管理人员";
                    }
                }                
            }
            else
            {
                lblComment.Text = "当前用户未注册，请确认后重试！";
                //Delay(3000);
                //lblComment.Text = "";
            }
        }

        public void Delay(int milliSecond)
        {
            int start = Environment.TickCount;
            while (Math.Abs(Environment.TickCount - start) < milliSecond)//毫秒
            {
                lblComment.Text = ""; //可执行某无聊的操作
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {

        }
    }
}